package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"math/cmplx"
	"os"
	"runtime"
	"strconv"
	"unicode/utf8"
)

var g_a = "aa"
var (
	ga = 1
	gb = "s"
)

func hex2bin(i int) string {
	if i == 0 {
		return "0"
	}
	result := ""
	for ; i > 0; i = (i - i%2) / 2 {
		result = strconv.Itoa(i%2) + result
	}
	return result
}
func chapterone() {
	fmt.Println("hello" + runtime.GOARCH)
	var i, b = 3, 4
	var s string = "abc"
	var x, y, z = 1, "s", true
	const c1 = 123
	g, h := "g", false
	fmt.Printf("%d %q\n", i, s)
	fmt.Println(b, x, y, z, g, h)
	c := 3 + 4i
	fmt.Println(cmplx.Abs(c))
	const ( //enum  _means skip
		apple = 1
		_
		banana = 2
		pear   = 3
	)
	const ( //enum  _means skip
		apple1 = iota
		_
		banana1
		pear1
	)
	if contents, err := ioutil.ReadFile("a.txt"); err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(contents)
	}
	//golang中的switch自带break，不需要写break
	//switch空 case bool
	score := 100
	switch {
	case score < 60:
		fmt.Printf("不合格")
	case 60 <= score && score <= 100:
		fmt.Println("合格")
	default:
		panic(fmt.Sprintf("出乎意料的值：%d", score))

	}
	fmt.Println(hex2bin(2))
	file, err := os.Open("a.txt")
	if err != nil {
		panic(err)
	}
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}
}

func change0(arr *[5]int) {
	arr[0] = 100
}
func change0_slice(arr []int) {
	arr[0] = 200
}

// 外部不能用:=
func main() {
	var arr1 [5]int
	arr1 = [...]int{1, 2, 4, 5, 6}
	/*
	  使用:=时要写出数组的初始值
	*/
	arr2 := [3]int{1, 2, 3}
	arr3 := [...]int{2, 4, 6, 7, 8}
	var grid = /* [3][2]int */ [3][2]int{
		{1, 2},
		{3, 4},
		{5, 6},
	}
	grid2 := make([][]int, 5)
	fmt.Println(arr1, arr2, grid, grid2)
	for i := 0; i < len(arr1); i++ {
		fmt.Println(arr1[i])
	}
	for i, v := range arr3 {
		fmt.Println(i, v)
	}
	s := arr3[1:4]
	fmt.Println(s, arr3[3:], arr3[:])
	/*
		go的数组是值传递，在另一个函数修改数组的值
		可以使用指针（但是要在参数列表指明数组长度并传入指针，
		或者使用切片，并传入切片
	*/
	change0(&arr3)
	fmt.Println(arr3)
	change0_slice(arr3[:])
	fmt.Println(arr3)
	/*
	   slice {ptr,len,cap}
	   len是当前slice的长度，cap是原数组的长度
	 ，将本slice再取slice可以取到len以外cap以内的部分
	*/
	s1 := append(s, 9)
	fmt.Println(arr3, s1)
	s1[1] = 300
	s2 := append(s1, 10)
	fmt.Println(arr3, s2)
	s3 := append(s2, 11)
	s3[1] = 0
	fmt.Println(arr3, s3)
	/*
	   当append超出cap的范围时，slice不再指向原来的array
	   修改slice中的值不再使原array变化
	*/
	s4 := []int{2, 4, 6, 8}
	s5 := make([]int, 10, 32)
	s6 := make([]int, 5, 16)
	fmt.Println(s5)
	copy(s5, s4)
	fmt.Println(s5)
	copy(s6, s5[3:])
	fmt.Println(s6)

	m := map[int]string{
		0: "a",
		1: "b",
		3: "c",
	}
	/*
	 hash map 顺序和写的顺序不一样
	 除slick map function的内建类型以及不含以上的struct都可作为key
	*/
	for k, v := range m {
		fmt.Println(k, v)
	}
	v1, ok := m[1]
	vn, okn := m[7]
	fmt.Println(v1, ok, vn, okn)
	delete(m, 1)
	/*
		取不存在的key会得到zero value
		第二个参数会返回false
	*/
	str := "yes我爱吃水果"
	for _, v := range []byte(str) {
		fmt.Printf("%c ", v)
	}
	fmt.Println()
	for _, v := range str { //return int32(rune)
		fmt.Printf("%c ", v)
	}
	fmt.Println()
	for _, v := range []rune(str) {
		fmt.Printf("%c ", v)
	}
	utf8.RuneCountInString(str) //return str lenth(international)
}
